package com.mvc.business.user.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/ajax")
public class AjaxController {
	
	/**
	 * ajax请求并自动组装json返回
	 * 返回到前台的数据为JSON对象，不需要再次转换
	 * 需要添加@ResponseBody注解
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/getJsonValue",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getJsonValue(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Integer v1 = Integer.parseInt(request.getParameter("v1"));
		Integer v2 = Integer.parseInt(request.getParameter("v2"));
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("result", v1+v2);
		return result;
	}
	
	/**
	 * ajax请求返回字符串数据
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/getValue",method=RequestMethod.GET)
	@ResponseBody
	public String getValue(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Integer v1 = Integer.parseInt(request.getParameter("v1"));
		Integer v2 = Integer.parseInt(request.getParameter("v2"));
		return v1+v2+"";
	}
	
	
}
